<template>
    <div>
        <form class="form-inline pull-left">
            <div class="form-group" style="padding-bottom:10px;">
                <span >查看部门</span>
                <select name="dept" id="dept" class="form-control"></select>
                <a class="btn btn-sm btn-theme" @click="research">查询</a>
            </div>
        </form>
        <ul class="nav nav-tabs" style="clear: both" role="tablist">
            <li role="presentation" class="active"><a href="#store" role="tab" data-toggle="tab">物料库存</a></li>
            <li role="presentation"><a href="#instock" role="tab" data-toggle="tab">入库记录</a></li>
            <li role="preaentation"><a href="#pickup" role="tab" data-toggle="tab">领用记录</a></li>
        </ul>
        <div class="tab-content">
            <div role="tabpanel" class="tab-pane  active" id="store">
                <table id="store_table"></table>
            </div>
            <div role="tabpanel" class="tab-pane" id="instock">
                <table id="instock_table"></table>
            </div>
            <div role="tabpanel" class="tab-pane" id="pickup">
                <table id="pickup_table"></table>
            </div>
        </div>
        <div class="modal fade" id="materialRecordModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title" id="myModalLabel">物料入库领用记录</h4>
                </div>
                <div class="modal-body">
                    <ul class="nav nav-tabs" role="tablist">
                        <li role="presentation" class="active"><a href="#mInstock" role="tab" data-toggle="tab">入库记录</a></li>
                        <li role="presentation"><a href="#mPickup" role="tab" data-toggle="tab">领用记录</a></li>
                    </ul>
                    <div class="tab-content">
                        <div role="tabpanel" class="tab-pane active" id="mInstock">
                            <table class="table" id="mInstockTable"></table>
                        </div>
                        <div role="tabpanel" class="tab-pane" id="mPickup">
                            <table class="table" id="mPickupTable"></table>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary">确定</button>
                </div>
                </div>
            </div>
</div>
    </div>
</template>
<script>
    export default {
        data() {
            return {
                currentRow: ''
            }
        },
        methods: {
            getDept() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/dept/fetchalllist',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid
                    },
                    dataType: 'json',
                    crossDomain: true,
                    async: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误');
                    },
                    success: function(retdata) {
                        var info_dept = retdata;
                        var cont = '<option value="0">全部</option>';

                        for (var i = 0; i < info_dept.Table2.length; i++) {
                            cont += '<option value="' + info_dept.Table2[i].fidno + '">' + info_dept.Table2[i].fdname + '</option>';
                        }
                        $('#dept').html(cont);
                    }
                });
            },
            getStoreTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/fetchstorebydept',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fdid': $('#dept').val()
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        var operateEvents = {
                            'click a[name="search"]': function(e, value, row, index) {
                                _this.currentRow = row
                                _this.showMaterialRecordModal()
                            }
                        }
                        $('#store_table').bootstrapTable('destroy')
                        $('#store_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fitemno',
                                title: '物料号',
                                halign: 'center'
                            }, {
                                field: 'fcgn',
                                title: '物料名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'fqty',
                                title: '库存数量',
                                halign: 'center'
                            }, {
                                field: 'operate',
                                title: '操作',
                                halign: 'center',
                                formatter: '<a class="btn btn-reddit btn-sm" name="search">查看入库、领用记录</a>',
                                events: operateEvents
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'store',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            // showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: false,
                            // radio: true,

                        });
                    }
                });
            },
            getInstockTable() {
                $.ajax({
                    url: 'http://192.168.1.66:808/api/instock/getallinstock',
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': $("#dept").val()
                    },
                    crossDomain: true,
                    async: true,
                    success(res) {
                        $('#instock_table').bootstrapTable('destroy');
                        $('#instock_table').bootstrapTable({
                            data: res.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '入库单号',
                            }, {
                                field: 'fdid',
                                title: '请购单号'
                            }, {
                                field: 'fitemno',
                                title: '物料号'
                            }, {
                                field: 'fcgn',
                                title: '物料名称'
                            }, {
                                field: 'fmodel',
                                title: '型号'
                            }, {
                                field: 'fprice',
                                title: '单价'
                            }, {
                                field: 'fqty',
                                title: '数量'
                            }, {
                                field: 'username',
                                title: '入库人'
                            }, {
                                field: 'entdate',
                                title: '入库日期'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            // clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: false,
                            // radio: true,
                            checkbox: true,
                        });
                    }
                })
            },
            getPickupTable() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/getallpickup',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fidno': $('#dept').val()
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误');
                    },
                    success: function(retdata) {
                        $('#pickup_table').bootstrapTable('destroy');
                        $('#pickup_table').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '领用单号',
                            }, {
                                field: 'fitemno',
                                title: '物料号'
                            }, {
                                field: 'fcgn',
                                title: '物料名称'
                            }, {
                                field: 'fmodel',
                                title: '型号'
                            }, {
                                field: 'fprice',
                                title: '单价'
                            }, {
                                field: 'fqty_abs',
                                title: '数量'
                            }, {
                                field: 'fnotes',
                                title: '领用备注',
                            }, {
                                field: 'username',
                                title: '领用人'
                            }, {
                                field: 'entdate',
                                title: '领用日期'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            //showFooter: true,
                            showColumns: true,
                            showRefresh: true,
                            // showToggle: true,
                            showPaginationSwitch: true,
                            //detailView: true,
                            // clickToSelect: true,
                            singleSelect: false,
                            checkboxHeader: false,
                            // radio: true,
                            checkbox: true,
                        });
                    }
                });
            },
            research() {
                this.getStoreTable()
                this.getPickupTable()
                this.getInstockTable()
            },
            showMaterialRecordModal() {
                var _this = this
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/fetchinstockbyitemno',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fdid': $('#dept').val(),
                        'fitemno': _this.currentRow.fitemno
                    },
                    dataType: 'json',
                    // async: false,
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#mInstockTable').bootstrapTable('destroy')
                        $('#mInstockTable').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '入库单号',
                                halign: 'center'
                            }, {
                                field: 'fdid',
                                title: '请购单号',
                                halign: 'center'

                            }, {
                                field: 'fitemno',
                                title: '物料号',
                                halign: 'center'

                            }, {
                                field: 'fcgn',
                                title: '物料名称',
                                halign: 'center'

                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'

                            }, {
                                field: 'fprice',
                                title: '单价',
                                halign: 'center'

                            }, {
                                field: 'fqty',
                                title: '数量',
                                halign: 'center'

                            }, {
                                field: 'entuser',
                                title: '入库人',
                                halign: 'center'

                            }, {
                                field: 'entdate',
                                title: '入库日期',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            showColumns: true,
                            showRefresh: true,
                            showPaginationSwitch: true,
                            singleSelect: false,
                            checkboxHeader: false,
                        });
                    }
                });
                $.ajax({
                    type: 'POST',
                    url: 'http://192.168.1.66:808/api/instock/fetchpickupbyitemno',
                    data: {
                        'token': localStorage.token,
                        'userid': localStorage.userid,
                        'fdid': $('#dept').val(),
                        'fitemno': _this.currentRow.fitemno
                    },
                    dataType: 'json',
                    crossDomain: true,
                    error: function(request) {
                        _this.$store.commit('showModal','连接错误')
                    },
                    success: function(retdata) {
                        $('#mPickupTable').bootstrapTable('destroy')
                        $('#mPickupTable').bootstrapTable({
                            data: retdata.Table2,
                            columns: [{
                                field: 'fidno',
                                title: '领用单号',
                                halign: 'center'
                            }, {
                                field: 'fitemno',
                                title: '物料号',
                                halign: 'center'
                            }, {
                                field: 'fcgn',
                                title: '物料名称',
                                halign: 'center'
                            }, {
                                field: 'fmodel',
                                title: '型号',
                                halign: 'center'
                            }, {
                                field: 'fprice',
                                title: '单价',
                                halign: 'center',
                                visible: false
                            }, {
                                field: 'fqty',
                                title: '数量',
                                halign: 'center'
                            }, {
                                field: 'fnotes',
                                title: '领用备注',
                                halign: 'center'
                            }, {
                                field: 'entuser',
                                title: '领用人',
                                halign: 'center'
                            }, {
                                field: 'entdate',
                                title: '领用日期',
                                halign: 'center'
                            }],
                            pagination: true,
                            paginationLoop: true,
                            onlyInfoPagination: false,
                            striped: true,
                            //sortOrder: 'desc',
                            sidePagination: 'client',
                            pageNumber: 1,
                            pageSize: 10,
                            pageList: [10, 25, 50, 100],
                            smartDisplay: true,
                            selectItemName: 'material',
                            search: true,
                            searchOnEnterKey: false,
                            strictSearch: false,
                            showHeader: true,
                            maintainSelected: true,
                            showColumns: true,
                            showRefresh: true,
                            showPaginationSwitch: true,
                            singleSelect: false,
                            checkboxHeader: false,
                        });
                    }
                });
                $('#materialRecordModal').modal('show')
            }
        },
        mounted() {
            this.getDept()
            this.getStoreTable()
            this.getPickupTable()
            this.getInstockTable()
        }
    }
</script>
<style>

</style>